package com.lp.util.iot;

import com.alibaba.fastjson.JSONObject;
import com.lp.bean.GranpTree;
import com.lp.bean.TServerUrl;
import com.lp.cache.CacheName;
import com.lp.cache.ProCacheUtil;
import com.lp.common.PulicCallUrl;
import com.lp.service.impl.BaseServiceImpl;
import com.lp.util.HttpBaseUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class LiveNVSUtil extends BaseServiceImpl {

    public String getLoginToken(TServerUrl tServerUrl) {
        try {
            String loginUrl = tServerUrl.getLogin_url().substring(0, tServerUrl.getLogin_url().indexOf("//") + 2) + tServerUrl.getIp() + ":" + tServerUrl.getPort() + PulicCallUrl.GET_TOKRN + "?username=" + tServerUrl.getUsername() + "&password=" + tServerUrl.getPassword();
            JSONObject json = HttpBaseUtils.connectionGetForObject(loginUrl);
            if (json != null) {
                String token = json.getJSONObject("LiveQing").getJSONObject("Body").getString("URLToken");
                return token;
            }
        } catch (Exception e) {
            log.error("网络请求异常");
            e.printStackTrace();
        }
        return null;
    }

    public List<GranpTree> getGranpTrees(List<TServerUrl> urlAll) {

        List<GranpTree> treeArrayList = new ArrayList<>();

        for (int i = 0; i < urlAll.size(); i++) {
            TServerUrl tServerUrl = urlAll.get(i);
            String url = tServerUrl.getLogin_url().substring(0, tServerUrl.getLogin_url().indexOf("//") + 2) + tServerUrl.getIp() + ":" + tServerUrl.getPort();

            // 获取登录token
            String loginToken = ProCacheUtil.getCache(CacheName.LOGIN_TOKEN, tServerUrl.getIp());
            if (StringUtils.isEmpty(loginToken)) {
                loginToken = getLoginToken(tServerUrl);
                ProCacheUtil.addCache(CacheName.LOGIN_TOKEN, tServerUrl.getIp(), loginToken);
            }

            // 获取树节点
            GranpTree groupTree = getGroupTree(url + PulicCallUrl.GET_GROUP_TREE + "&token=" + loginToken);
            groupTree.setIp(tServerUrl.getIp());
            setGroupByIp(groupTree, tServerUrl.getIp());

            if (treeArrayList.size() == 0) {
                treeArrayList.add(groupTree);
            } else {
                treeArrayList.get(0).getChildren().addAll(groupTree.getChildren());
            }
        }
        return treeArrayList;
    }

    public GranpTree getGroupTree(String url) {
        String jsonstr = HttpBaseUtils.connectionGet(url);
        List<GranpTree> granpTreeList = JSONObject.parseArray(jsonstr, GranpTree.class);
        if (granpTreeList != null && granpTreeList.size() != 0) {
            return granpTreeList.get(0);
        } else {
            throw new IllegalArgumentException("拿取分组树数据异常");
        }
    }

    public void setGroupByIp(GranpTree groupTree, String ip) {
        if (groupTree.getChildren() != null && groupTree.getChildren().size() != 0) {
            for (GranpTree child : groupTree.getChildren()) {
                child.setIp(ip);
                setGroupByIp(child, ip);
            }
        }
    }

    public String getServerToken(String ip) {
        String obj = ProCacheUtil.getCache(CacheName.LOGIN_TOKEN, ip);
        if (StringUtils.isNotEmpty(obj)) {
            return obj;
        }
        TServerUrl info = new TServerUrl();
        info.setIp(ip);
        TServerUrl ipObj = dao.selectOne("TServerUrl.selectOne", info);

        String loginToken = null;
        String token = getLoginToken(ipObj);
        if (token != null) {
            loginToken = token;
        }
        if (loginToken == null) {
            throw new RuntimeException("远程服务异常ip：" + ip);
        }
        ProCacheUtil.addCache(CacheName.LOGIN_TOKEN, ip, loginToken);
        return loginToken;
    }

}
